package com.li.taskcenter.controller.quartz;

import com.li.configserve.domain.gloabreturn.ResponseInfo;
import com.li.configserve.controller.BaseController;
import com.li.taskcenter.domain.quartz.QuartzBean;
import com.li.taskcenter.service.quartz.QuartzService;
import com.li.taskcenter.utils.QuartzUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description：定时任务启动停止
 * @Author：LiDong
 * @Create：2020/12/15
 * @Version：1.0.0
 */
@RestController
@RequestMapping("/quartz")
public class QuartzController extends BaseController {

    /**
     * 注入任务调度
     **/
    @Resource
    private Scheduler scheduler;

    @Autowired
    private QuartzService quartzService;

    @RequestMapping("/createJob")
    public ResponseInfo createJob()  {
        return quartzService.createJob(scheduler,getParamMap(request),getCurrentUser());
    }

    @RequestMapping("/pauseJob")
    public ResponseInfo  pauseJob()  {
        return quartzService.pauseJob(scheduler,getParamMap(request),getCurrentUser());
    }

    /**
     * 执行任务一次
     * @return
     */
    @RequestMapping("/runOnce")
    public ResponseInfo  runOnce()  {
        return quartzService.runOnce(scheduler,getParamMap(request),getCurrentUser());
    }

    @RequestMapping("/resume")
    public ResponseInfo  resume()  {
        return quartzService.resumeScheduleJob(scheduler,getParamMap(request),getCurrentUser());
    }

    @RequestMapping("/update")
    @ResponseBody
    public String  update(QuartzBean quartzBean)  {
        try {
            //进行测试所以写死
            quartzBean.setJobClass("com.li.taskcenter.controller.quartz.MyTask1");
            quartzBean.setJobName("test1");
            quartzBean.setCronExpression("10 * * * * ?");
            QuartzUtils.updateScheduleJob(scheduler,quartzBean);
        } catch (Exception e) {
            return "启动失败";
        }
        return "启动成功";
    }

}
